package com.gyx.lottery.controller;

import com.gyx.lottery.common.errorcode.ControllerErrorCodeConstants;
import com.gyx.lottery.common.exception.ControllerException;
import com.gyx.lottery.common.pojo.CommonResult;
import com.gyx.lottery.common.utils.JacksonUtil;
import com.gyx.lottery.controller.param.PriCreParam;
import com.gyx.lottery.controller.param.PageParam;
import com.gyx.lottery.controller.result.PrizeListResult;
import com.gyx.lottery.service.PictureService;
import com.gyx.lottery.service.PrizeService;
import com.gyx.lottery.service.dto.PageListDTO;
import com.gyx.lottery.service.dto.PrizeDTO;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.stream.Collectors;

@RestController
public class PrizeController {
private static final Logger logger= LoggerFactory.getLogger(PrizeController.class);
@Autowired
    private PictureService pictureService;
@Autowired
    private PrizeService prizeService;
@RequestMapping("/upload/pic")
    public String upload(MultipartFile file){
    return pictureService.savePicture(file);
}
@RequestMapping("/prize/create")
    public CommonResult<Long> CreatePrize(@Valid @RequestPart("param") PriCreParam param, @RequestPart("prizePic")
                                          MultipartFile prizePic)
{
    logger.info("CreatePrize 中 接收参数为:PriCreParam:{}", JacksonUtil.writeVauleAsString(param));

    return CommonResult.success(prizeService.createPrize(param,prizePic));
}
@RequestMapping("/prize/find-list")
    public CommonResult<PrizeListResult> getPrizeList( PageParam param){
    logger.info("getPrizeList 中 PrizeListParam:{}",JacksonUtil.writeVauleAsString(param));
       PageListDTO<PrizeDTO> pageListDTO= prizeService.findPrizeList(param);
       return CommonResult.success(converToPrizeListResult(pageListDTO));
}

    private PrizeListResult converToPrizeListResult(PageListDTO<PrizeDTO> pageListDTO) {
    if(null == pageListDTO){
        throw new ControllerException(ControllerErrorCodeConstants.PRIZE_GETLIST_ERROR);
    }
      PrizeListResult result=new PrizeListResult();
      result.setTotal(pageListDTO.getTotal());
      result.setRecords(pageListDTO.getRecords().
              stream().
              map(prizeDTO->{
                  PrizeListResult.PrizeInfo prizeInfo=new PrizeListResult.PrizeInfo();
                  prizeInfo.setPrizeId(prizeDTO.getPrizeId());
                  prizeInfo.setPrizeName(prizeDTO.getPrizeName());
                  prizeInfo.setPrice(prizeDTO.getPrice());
                  prizeInfo.setDescription(prizeDTO.getDescription());
                  prizeInfo.setImageUrl(prizeDTO.getImageUrl());
                  return prizeInfo;
              }).collect(Collectors.toList()));
    return result;
    }


}
